package com.example.gokuplayalong.service.publicclient.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.gokuplayalong.bases.base.AppParams;
import com.example.gokuplayalong.bases.base.ExceptionHandler;
import com.example.gokuplayalong.service.publicclient.service.PublicClientService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/public/client")
public class PublicClientController {

    @Autowired
    PublicClientService publicClientService;

    @Async("AsyncThread")
    @RequestMapping("/login")
    public CompletableFuture<String> login(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.login(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/getKeyParams")
    public CompletableFuture<String> getKeyParams(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.getKeyParams(s));
    }


    @Async("AsyncThread")
    @RequestMapping("/pushRid")
    public CompletableFuture<String> pushRid(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.pushRid(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/checkUpgrade")
    public CompletableFuture<String> checkUpgrade(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.checkUpgrade(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/buildOrderInfo")
    public CompletableFuture<String> buildOrderInfo(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.buildOrderInfo(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/alipayHandler")
    public CompletableFuture<String> alipayHandler(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.alipayHandler(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/sendMessage")
    public CompletableFuture<String> sendMessage(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.sendMessage(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/merchantsCreate")
    public CompletableFuture<String> merchantsCreate(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.merchantsCreate(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/getMerchantInfo")
    public CompletableFuture<String> getMerchantInfo(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.getMerchantInfo(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/modifyNickName")
    public CompletableFuture<String> modifyNickName(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.modifyNickName(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/getUserList")
    public CompletableFuture<String> getUserList(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.getUserList(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/getChatPageList")
    public CompletableFuture<String> getChatPageList(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.getChatPageList(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/getChatList")
    public CompletableFuture<String> getChatList(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.getChatList(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/modifyMerchantInfo")
    public CompletableFuture<String> modifyMerchantInfo(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.modifyMerchantInfo(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/modifyMerchantSkills")
    public CompletableFuture<String> modifyMerchantSkills(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.modifyMerchantSkills(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/getSkillsList")
    public CompletableFuture<String> getSkillsList(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.getSkillsList(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/orderCreate")
    public CompletableFuture<String> orderCreate(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.orderCreate(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/getOrderList")
    public CompletableFuture<String> getOrderList(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.getOrderList(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/orderStart")
    public CompletableFuture<String> orderStart(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.orderStart(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/orderFinish")
    public CompletableFuture<String> orderFinish(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.orderFinish(s));
    }

    @Async("AsyncThread")
    @RequestMapping("/installSubmit")
    public CompletableFuture<String> installSubmit(@RequestParam("msg") String map) {
        String s = publicClientService.decrypt(map);
        return CompletableFuture.completedFuture(publicClientService.installSubmit(s));
    }


    @Async("AsyncThread")
    @RequestMapping("/uploadFile")
    public CompletableFuture<String> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("filename") String filename) {

        String result = "failed";
        try {
            String filePath = "";
            if (AppParams.getInstance().Platform == "window") {
//                imagePath = AppBean.getInstance().WindowUploadImgDir + "\\" + userId + "_" + index + "_" + num + "_" + note + "_" + orderNum + ".png";
                filePath = AppParams.getInstance().WindowUploadLogDir + "\\" + filename;
            }
            else {
//                imagePath = AppBean.getInstance().LinuxUploadImgDir + "/" + userId + "_" + index + "_" + num + "_" + note + "_" + orderNum + ".png";
                filePath = AppParams.getInstance().LinuxUploadLogDir + "/" + filename;

            }

            File targetFile = new File(filePath);
            if (!targetFile.getParentFile().exists()) { //注意，判断父级路径是否存在
                targetFile.getParentFile().mkdirs();
            }
            System.out.println(filePath);

            FileUtils.copyInputStreamToFile(file.getInputStream(), targetFile);
            result = "success";
        } catch (Exception e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return CompletableFuture.completedFuture(result);
        }
        return CompletableFuture.completedFuture(result);
    }


    @Async("AsyncThread")
    @PostMapping("/uploadImage")
    public CompletableFuture<String> uploadImage(@RequestParam("file") MultipartFile file, @RequestParam("filename") String filename) {
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");
        msgToClient.put("name", "uploadImage");
        try {
            String imagePath = "";
            if (AppParams.getInstance().Platform.equals("window")) {
//                imagePath = AppBean.getInstance().WindowUploadImgDir + "\\" + userId + "_" + index + "_" + num + "_" + note + "_" + orderNum + ".png";
                imagePath = AppParams.getInstance().WindowUploadImgDir + "\\" + filename;
            }
            else {
//                imagePath = AppBean.getInstance().LinuxUploadImgDir + "/" + userId + "_" + index + "_" + num + "_" + note + "_" + orderNum + ".png";
                imagePath = AppParams.getInstance().LinuxUploadImgDir + "/" + filename;
            }
            File targetFile = new File(imagePath);
            if (!targetFile.getParentFile().exists()) { //注意，判断父级路径是否存在
                targetFile.getParentFile().mkdirs();
            }
            else {
                targetFile.delete();
            }

            FileUtils.copyInputStreamToFile(file.getInputStream(), targetFile);

            msgToClient.put("result", "成功");
            System.out.println(msgToClient.toJSONString());
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
        }

        return CompletableFuture.completedFuture(msgToClient.toJSONString());
    }

    @Async("AsyncThread")
    @PostMapping("/alipayCallback")
    public CompletableFuture<String> alipayCallback(@RequestParam("receipt_amount") String receipt_amount, @RequestParam(value = "body", defaultValue = "") String body, @RequestParam("trade_status") String trade_status, @RequestParam("sign_type") String sign_type, @RequestParam("sign") String sign, @RequestParam("out_trade_no") String out_trade_no) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("out_trade_no", out_trade_no);
        map.put("sign", sign);
        map.put("sign_type", sign_type);
        map.put("receipt_amount", receipt_amount);
        map.put("body", body);
        map.put("trade_status", trade_status);

        return CompletableFuture.completedFuture(publicClientService.alipayCallback(map));
    }

}
